package com.bcx.controller;

import com.bcx.pojo.Detail;
import com.bcx.service.DetailService;
import com.bcx.tool.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DetailController {
    @Resource
    DetailService detailService;
    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Detail>>selectEntry(@RequestParam(value = "categoryid",required = false,defaultValue = "0")Integer categoryid
            , @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum
    ){
        PageInfo<Detail> pageInfo=detailService.selectAll(categoryid,pageNum);
        return ResultEntity.successWithoutData(pageInfo);
    }
    @RequestMapping("/saveEntry")
    public ResultEntity<String> saveEntry(@RequestBody Detail detail){
        try {
            Integer integer=0;
            if (detail.getId()!=null){
                integer=detailService.update(detail);
            }else {
                integer=detailService.update(detail);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }
    //根据id查询
    @RequestMapping("/getEntryById")
    public ResultEntity<Detail>getEntryById(Integer id){
        Detail detail=detailService.getById(id);
        return ResultEntity.successWithoutData(detail);
    }
}
